let app = getApp()
let ble = require("ble")

let code_list = []
let code_list_index = -1

let code  = ""
let code_index = -1

let cp = ""

let timeOut = ""

// 播放完一个字母的电码后的返回函数
let code_done_callback = ""

// 播放标识符
let flag_play = false

let play = () => {
	// console.log("play")
	app.data.common_store.cp.seek(0)
	app.data.common_store.cp.volume = 0
	app.data.common_store.cp.play()
	
}
let stop = () => {
	clearTimeout(timeOut)
	app.data.common_store.cp.stop()
	// app.data.common_store.cp.pause()
	// app.data.common_store.cp.volume = 0
}

let p_ios = () => {
	app.data.common_store.cp.volume = 0
}
let g_ios = () => {
	app.data.common_store.cp.volume = 1
}

let p_android = () => {
	app.data.common_store.cp.volume = 0
	app.data.common_store.cp.stop()
	
}
let g_android = () => {
	app.data.common_store.cp.volume = 1	
	app.data.common_store.cp.play()
	
}

let p = () => {
}
let g = () => {
}

let init_cp = () => {
	if(app.data.common_store.sys_info.system.substr(0, 3) == "iOS") {
		// app.data.common_store.cp.src = "/data/di_4.mp3"
		app.data.common_store.cp.src = "https://6469-dida-1301153571.tcb.qcloud.la/mp3/d11.mp3?sign=fa963aa42b3ec861bf646fc0fa1cae32&t=1608564144"
		app.data.common_store.cp.loop = true
		p = p_ios
		g = g_ios
	}
	else {
		// app.data.common_store.cp.src = "/data/d9.mp3"
		app.data.common_store.cp.src = "https://6469-dida-1301153571.tcb.qcloud.la/mp3/d9.mp3?sign=283bcf3ce8955df6cf05d7bb854c38cd&t=1608562525"
		p = p_android
		g = g_android
	}
}


// 播放一组电码
let play_c = (c) => {
	if(flag_play){
		if(c == ".") {
			// play()
			g()
			timeOut = setTimeout(get_next_c.bind(this, c), app.data.common_store.settings.play_u_time)
		}
		else if(c == "-") {
			g()
			timeOut = setTimeout(get_next_c.bind(this, c), app.data.common_store.settings.play_u_time * 2.1)
		}
		else if(c == "p") {
			// stop()
			p()
			timeOut = setTimeout(get_next_c.bind(this, c), app.data.common_store.settings.play_u_time / 4)
		}
		else if(c == "s") {
			// stop()
			p()
			timeOut = setTimeout(get_next_c.bind(this, c), app.data.common_store.settings.play_u_time * 1.5)
		}
	}
	else {
		stop()
		clear_time_out()
		return
	}
}

let clear_time_out = () => {
	if(timeOut != "") {
		clearTimeout(timeOut)
		timeOut = ""
	}
}

let get_next_c = (c = "") => {
	// stop()
	p()
	clear_time_out()
	if(c == "s") {
		code = ""
		code_index = -1
		if(code_done_callback != "") {
			code_done_callback()
		}
		get_next_code()
		return;
	}
	if(code_index == code.length -1) {
		play_c("s")
	}
	else if(c == "-" || c == "."){
		play_c("p")
	}
	else  {
		code_index++
		if(code_index < code.length) {
			play_c(code[code_index])
		}
	}
}

let get_next_code = () => {
	code_list_index ++ 
	if (code_list_index < code_list.length) {
		code = code_list[code_list_index]
		code_index = -1
		get_next_c()
	}
	else {
		code_list_index = -1
	}
}


// c_list	要播放的字母电码列表
// callback_fn 播放完一个字母的电码后执行的函数
let play_code_list = (c_list, callback_fn = "") => {
	code_list = c_list
	flag_play = true
	code_done_callback = callback_fn
	play()
	get_next_code()
}

// 停止播放
let cut = () => {
	flag_play = false
	stop()
	code_list = []
	code_list_index = -1
	code = ""
	code_index = -1
}

let ble_play_code = (c) => {
	// let code_str = code_list.join(" ")
	ble.send("plc:" + c)
}

let ble_play_code_list = (code_list) => {
	let code_str = code_list.join(" ")
	ble.send("plc:" + code_str)
}
let stop_ble_play_code = () => {
	ble.send("stp")
}


module.exports = {
	play: play,
	init_cp: init_cp,
	play_c: play_c,
	play_code_list: play_code_list,
	cut: cut,
	ble_play_code: ble_play_code,
	stop_ble_play_code: stop_ble_play_code,
	ble_play_code_list: ble_play_code_list,
}